package com.example.flight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.example.flight.dao.FlightInformationDao;
import com.example.flight.entity.FlightInformationEntity;
import com.example.flight.service.FlightInformationService;


import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service("flightInformationService")
public class FlightInformationServiceImpl extends ServiceImpl<FlightInformationDao, FlightInformationEntity> implements FlightInformationService {

@Autowired
FlightInformationDao flightInformationDao;
    @Override
    public List<FlightInformationEntity> searchList(String departure, String destination, Date departureDate) {
        LambdaQueryWrapper<FlightInformationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotEmpty(departure),FlightInformationEntity::getDeparture,departure)
                .eq(Strings.isNotEmpty(destination),FlightInformationEntity::getDestination,destination)
                .and(i->i.gt(FlightInformationEntity::getEconomyClassAvailable,0)
                        .or()
                        .gt(FlightInformationEntity::getBusinessClassAvailable,0));
        if (departureDate != null) {
            Date now = new Date();
            if (departureDate.before(now))departureDate=now;
            queryWrapper.between(FlightInformationEntity::getDepartureTime,departureDate,new Date(departureDate.getTime()+1000*60*60*24-1));
        }else {
            queryWrapper.gt(FlightInformationEntity::getDepartureTime,new Date());
        }
        return  flightInformationDao.selectList(queryWrapper);
    }
    @Override
    public List<FlightInformationEntity> searchList2(String flightNum,String departure, String destination, Date departureDate) {
        LambdaQueryWrapper<FlightInformationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotEmpty(departure),FlightInformationEntity::getDeparture,departure)
                .eq(Strings.isNotEmpty(destination),FlightInformationEntity::getDestination,destination)
                .like(Strings.isNotEmpty(flightNum),FlightInformationEntity::getFlightNum,flightNum);
        if (departureDate != null) {
            queryWrapper.between(FlightInformationEntity::getDepartureTime,departureDate,new Date(departureDate.getTime()+1000*60*60*24-1));
        }
        return  flightInformationDao.selectList(queryWrapper);
    }
}